layui.use('element', function () {
    var $ = layui.jquery, element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    //触发事件
    var active = {
        tabDelete: function (othis) {
            othis.addClass('layui-btn-disabled');
            $("li[lay-id='home']").find("i.layui-icon").remove();
        }
    };

    $('.site-demo-active').on('click', function () {
        var othis = $(this), type = othis.data('type');
        active[type] ? active[type].call(this, othis) : '';
    });

    switchFun("#modules/front/index.html");
    element.on('tab(navFilter)', function () {
        switchFun(this.getAttribute('lay-id'));
        var url = this.getAttribute('lay-id');
        vm.main = url.replace('#', '');
    });

    $(".sidebar-menu a ").click(function () {
        var url = $(this).attr("data-href");
        if (url == '#modules/front/myproject.html?id=null' || url == '#modules/front/myproject.html?id=') {
            layer.msg("工程未打开，无法进行操作", {icon: 1});
        } else {
            vm.main = url.replace('#', '');
            if ($("li[lay-id='" + $(this).attr("data-href") + "']").length == 0) {
                element.tabAdd('navFilter', {
                    title: $(this).text(),
                    id: $(this).attr("data-href")
                });
                element.tabChange('navFilter', $(this).attr("data-href"));
            } else {
                element.tabChange('navFilter', $(this).attr("data-href"));
            }
            switchFun($(this).attr("data-href"));
        }
    })


});

function switchFun(url) {
    console.log("dssf:" + url);
    if (url == "#") {
        return;
    }
    $(".treeview-menu li").removeClass("active");
    $(".sidebar ul li").removeClass("active");
    $("a[data-href='" + url + "']").parents("li").addClass("active");
    vm.navTitle = $("a[data-href='" + url + "']").eq(0).text();

}

//生成菜单
var menuItem = Vue.extend({
    name: 'menu-item',
    props: {item: {}},
    template: [
        '<li>',
        '	<a v-if="item.type === 0" href="javascript:;">',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span>{{item.name}}</span>',
        '		<i class="fa fa-angle-left pull-right"></i>',
        '	</a>',
        '	<ul v-if="item.type === 0" class="treeview-menu">',
        '		<menu-item :item="item" v-for="item in item.list"></menu-item>',
        '	</ul>',

        '	<a v-if="item.type === 1 && item.parentId === 0" :href="\'#\'+item.url">',
        '		<i v-if="item.icon != null" :class="item.icon"></i>',
        '		<span>{{item.name}}</span>',
        '	</a>',

        '	<a v-if="item.type === 1 && item.parentId != 0" :href="\'#\'+item.url"><i v-if="item.icon != null" :class="item.icon"></i><i v-else class="fa fa-circle-o"></i> {{item.name}}</a>',
        '</li>'
    ].join('')
});

//iframe自适应
$(window).on('resize', function () {
    var $content = $('.content');
    $content.height($(this).height() - 120);
    $content.find('iframe').each(function () {
        $(this).height($content.height());
    });
}).resize();

//注册菜单组件
Vue.component('menuItem', menuItem);
var vm = new Vue({
    el: '#rrapp',
    data: {
        user: {},
        menuList: {},
        main: "modules/front/projects.html",
        password: '',
        newPassword: '',
        navTitle: "控制台",
        projectId: '',
        isAdmin: true,
        project: {},
        process: 0,
        sysAttrConfigEntity: {},
        sysToolEntity: {}
    },
    created: function () {

    },
    methods: {
        getMenuList: function (event) {
            $.getJSON("sys/menu/nav?_" + $.now(), function (r) {
                vm.menuList = r.menuList;
            });
        },
        getUser: function () {
            $.getJSON("sys/user/info?_" + $.now(), function (r) {
                vm.user = r.user;
                vm.sysAttrConfigEntity = r.sysAttrConfigEntity;
                vm.sysToolEntity = r.sysToolEntity;
                $('#toolUrl').attr('data-href',vm.sysToolEntity.url);
                vm.user.space = Math.floor(r.user.space*100 / 1024)/100;
                vm.process = vm.user.space / vm.sysAttrConfigEntity.space * 100;
                if (vm.user.username == "admin") {
                    vm.isAdmin = true;
                } else {
                    vm.isAdmin = false;
                }
            });
        },
        updatePassword: function () {
            layer.open({
                type: 1,
                skin: 'layui-layer-molv',
                title: "修改密码",
                area: ['550px', '270px'],
                shadeClose: false,
                content: jQuery("#passwordLayer"),
                btn: ['修改', '取消'],
                btn1: function (index) {
                    var data = "password=" + vm.password + "&newPassword=" + vm.newPassword;
                    $.ajax({
                        type: "POST",
                        url: "sys/user/password",
                        data: data,
                        dataType: "json",
                        success: function (result) {
                            if (result.code == 0) {
                                layer.close(index);
                                layer.alert('修改成功', function (index) {
                                    location.reload();
                                });
                            } else {
                                layer.alert(result.msg);
                            }
                        }
                    });
                }
            });
        },
        donate: function () {
            layer.open({
                type: 2,
                title: false,
                area: ['806px', '467px'],
                closeBtn: 1,
                shadeClose: false,
                content: ['http://cdn.renren.io/donate.jpg', 'no']
            });
        },

        saveProject: function () {
            if (vm.projectId != '' && vm.projectId != null) {
                $("#frame")[0].contentWindow.vm.saveBlock(null);
            }
        },

        //导出工程
        exportProject: function () {
            var obj = $("#frame").contents().find("#id");
            console.log(obj.val());
            if (!obj.val()) {
                //layer.alert("未打开工程工，无法导出！");
            } else {
                window.location.href = "ede/project/export?id=" + obj.val();
            }
            /*$.ajax({
                type: "POST",
                url: "ede/project/export?id="+obj.val(),
                data: {},
                dataType: "json",
                success: function (result) {
                    if (result.code == 0) {

                    } else {

                    }
                }
            });*/
        },

        //添加选项卡
        addTab: function (name, url) {
            if (layui.jquery(".layui-tab-title li[lay-id='" + url + "']").length > 0) {
                //选项卡已经存在
                layui.element.tabChange('navFilter', url);
                //layer.msg('切换-' + name)
            } else {
                //动态控制iframe高度
                var tabheight = layui.jquery(window).height() - 95;
                //contentTxt = '<iframe src="' + url + '" scrolling="no" width="100%" height="' + (tabheight) + 'PX"></iframe>';
                contentTxt = url;
                //新增一个Tab项
                layui.element.tabAdd('navFilter', {
                    title: name,
                    //content: contentTxt,
                    id: url
                })
                //切换刷新
                layui.element.tabChange('navFilter', url);
            }
        },
        attrCss: function () {
            $('#attrStr').css('display', 'block');
            $('#attrVal').css('display', 'block');
        },
        refresh: function () {
            $('#frame').attr('src', $('#frame').attr('src'));
        },
        getProjectById: function (id) {
            $.get("ede/project/info/"+id, function(r){
                vm.project = r.project;
                if(r.project){
                    vm.projectId = id;
                }
            });
            parent.vm.attrCss();
        },
    },
    created: function () {
        this.getMenuList();
        this.getUser();
        $('#attrStr').css('display', 'none');
        $('#attrVal').css('display', 'none');
    },
    updated: function () {
        //路由
        var router = new Router();
        routerList(router, vm.menuList);
        router.start();
    }


});


function routerList(router, menuList) {
    for (var key in menuList) {
        var menu = menuList[key];
        if (menu.type == 0) {
            routerList(router, menu.list);
        } else if (menu.type == 1) {
            router.add('#' + menu.url, function () {
                var url = window.location.hash;


            });
        }
    }
}